package com.example.administrator.managesystem.activity.firstpage.wiki;

import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.base.BaseActivity;
import com.example.administrator.managesystem.bean.WikiDetailBean;
import com.example.administrator.managesystem.bean.WikiListBean;
import com.example.administrator.managesystem.presenter.firstpage.FirstPagePresenter;
import com.example.administrator.managesystem.ui.WikiView;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.example.administrator.managesystem.view.RepostDialog;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.List;

public class WikiDetailActivity extends BaseActivity implements WikiView {
    private TextView title,author,view_num,time,collect_num,content_tv;
    private ImageView collect_iv,repost_iv;
    private LinearLayout content;
    private SpannableString spanText;
    private FirstPagePresenter firstPagePresenter;
    private String url;
    @Override
    public void initView() {
        showfloatButton(true,true);
        firstPagePresenter=new FirstPagePresenter(this);
        title=findViewById(R.id.title);
        author=findViewById(R.id.author);
        view_num=findViewById(R.id.view_num);
        time=findViewById(R.id.time);
        content=findViewById(R.id.content);
        collect_num=findViewById(R.id.collect_num);
        collect_iv=findViewById(R.id.collect_iv);
        repost_iv=findViewById(R.id.repost_iv);
    }

    @Override
    public void doBusiness() {
        final WikiDetailBean.DataBean dataBean = (WikiDetailBean.DataBean) getIntent().getSerializableExtra("dataBean");
        title.setText(dataBean.getTitle());
        author.setText(dataBean.getAuthor());
        time.setText(dataBean.getRelease_time());
        url=dataBean.getShare_url();
        Log.e("msg","dataBean.isIs_collection():"+dataBean.isIs_collection());
        if(dataBean.isIs_collection()){
            collect_iv.setImageResource(R.drawable.collect_seleted);
        }
        collect_num.setText(dataBean.getCollection_num()+"人已收藏");
        for(int i=0;i<dataBean.getContent().size();i++){
            spanText = new SpannableString(dataBean.getContent().get(i));
            content_tv = new TextView(this);
            content_tv.setLineSpacing(1.5f,1.5f);
            content_tv.setTextSize(14);
            content_tv.setTextColor(getResources().getColor(R.color.black));

                //若需自定义TextAppearance，可以在系统样式上进行修改
                spanText.setSpan(new TextAppearanceSpan(this, android.R.style.TextAppearance_DeviceDefault_Medium),
                        0, 0, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);

            content_tv.append("\n");
            content_tv.append(spanText);
            content.addView(content_tv);
        }
    collect_iv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        firstPagePresenter.collectArticleRequest(ShareUtil.getUserId(WikiDetailActivity.this), dataBean.get_id(),ShareUtil.getUserToken(WikiDetailActivity.this),WikiDetailActivity.this);
        }
    });
        repost_iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final RepostDialog repostDialog=new RepostDialog(WikiDetailActivity.this);
                repostDialog.setOnCancelListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        repostDialog.dismiss();
                    }
                });
                repostDialog.setOnFriendCircleListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstPagePresenter.repostArticleRequest(ShareUtil.getUserId(WikiDetailActivity.this),dataBean.get_id(),ShareUtil.getUserToken(WikiDetailActivity.this),0,WikiDetailActivity.this);
                        repostDialog.dismiss();
                    }
                });
                repostDialog.setOnQQListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstPagePresenter.repostArticleRequest(ShareUtil.getUserId(WikiDetailActivity.this),dataBean.get_id(),ShareUtil.getUserToken(WikiDetailActivity.this),1,WikiDetailActivity.this);
                        repostDialog.dismiss();
                    }
                });
                repostDialog.setOnQQZoneListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstPagePresenter.repostArticleRequest(ShareUtil.getUserId(WikiDetailActivity.this),dataBean.get_id(),ShareUtil.getUserToken(WikiDetailActivity.this),2,WikiDetailActivity.this);
                        repostDialog.dismiss();
                    }
                });

                repostDialog.setOnWechatListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        firstPagePresenter.repostArticleRequest(ShareUtil.getUserId(WikiDetailActivity.this),dataBean.get_id(),ShareUtil.getUserToken(WikiDetailActivity.this),3,WikiDetailActivity.this);
                        repostDialog.dismiss();
                    }
                });
                repostDialog.show();
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_wiki_detail;
    }

    @Override
    public void showWikiList(List<WikiListBean.DataBean> list,int type) {

    }

    @Override
    public void showWikiDetail(WikiDetailBean.DataBean dataBean) {

    }

    @Override
    public void updateCollectUI() {
      collect_iv.setImageResource(R.drawable.collect_seleted);
    }

    @Override
    public void updateRepost(int type) {
        UMImage thumb = new UMImage(this,R.drawable.app_icon);
        UMWeb web = new UMWeb(url);
        web.setTitle(title.getText().toString());//标题
        web.setDescription(title.getText().toString());
        web.setThumb(thumb);  //缩略图
        switch(type){
            case 0:
                //;
                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).withMedia(web)
                        .setCallback(shareListener).share();
                break;
            case 1:
                new ShareAction(this).setPlatform(SHARE_MEDIA.QQ).withMedia(web)
                        .setCallback(shareListener).share();
                break;
            case 2:
                new ShareAction(this).setPlatform(SHARE_MEDIA.QZONE).withMedia(web)
                        .setCallback(shareListener).share();
                break;
            case 3:

                new ShareAction(this).setPlatform(SHARE_MEDIA.WEIXIN).withMedia(web)
                        .setCallback(shareListener).share();
                break;


        }


    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
        }
        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(WikiDetailActivity.this,"分享成功！",Toast.LENGTH_LONG).show();
        }
        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(WikiDetailActivity.this,"分享成功！"+t.getMessage(),Toast.LENGTH_LONG).show();
        }
        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(WikiDetailActivity.this,"分享取消！",Toast.LENGTH_LONG).show();
        }
    };
}
